﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Spore.Validator;

namespace Client
{
    public partial class FrmPublishNotice : BasePopForm
    {
        public string OPERTYPE = "ADD";
        public DataRowView row = null;
        public FrmPublishNotice()
        {
            InitializeComponent();
            this.NecessaryRemark(this.txtTitle, this.txtContent);
            this.FormValidator.SetControlValitors(txtTitle, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(txtContent, ValidatorDefaults.NotNull);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!this.FormValidator.ValidAll()) return;
            QunYing.BLL.Notice notice = new QunYing.BLL.Notice();
            QunYing.Model.Notice model = new QunYing.Model.Notice();
            model.公告标题 = txtTitle.Text.Trim();
            model.公告内容 = txtContent.Text.Trim();
            model.是否过期 = cbxDue.Checked;
            if (OPERTYPE == "ADD")
            {
                model.发布时间 = DateTime.Now;
                model.显示顺序 = 1;

                if (notice.Add(model) > 0)
                {
                    GlobalInfo.MsgBoxShow("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    FrmNoticeManager.frm.QueryData();
                    this.Close();
                }
                else
                {
                    GlobalInfo.MsgBoxShowError("保存失败!");
                }
            }
            else if (OPERTYPE == "EDIT")
            {
                model.公告ID = Convert.ToInt32(row["公告ID"]);
                model.发布时间 = Convert.ToDateTime(row["发布时间"]);
                if (row["是否过期"].ToString() != "")
                {
                    model.显示顺序 = Convert.ToInt32(row["是否过期"]);
                }
                bool b = notice.Update(model);
                if (b)
                {
                    GlobalInfo.MsgBoxShow("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    FrmNoticeManager.frm.QueryData();
                    this.Close();
                }
                else
                {
                    GlobalInfo.MsgBoxShowError("保存失败!");
                }
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmPublishNotice_Load(object sender, EventArgs e)
        {
            if (OPERTYPE == "EDIT" && row != null)
            {
                BindInfo();
            }
        }

        private void BindInfo()
        {
            txtTitle.Text = row["公告标题"].ToString();
            txtContent.Text = row["公告内容"].ToString();
            cbxDue.Checked = row["是否过期"]==null? false : Convert.ToBoolean(row["是否过期"]);
        }
    }
}